package com.winenine999.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winenine999.model.Syauthority;
import com.winenine999.service.ISyauthorityService;

@Controller
@RequestMapping(value="/permission")
public class PermissionController {
	@Autowired
	private ISyauthorityService syauthorityService;
	/**
	 * 进入权限页面
	 * @return
	 */
	@RequestMapping(value="/permission",method=RequestMethod.GET)
	public String permission(){
		
		return "permission/permission";
	}
	/**
	 * 改权限名
	 * @param syauthority
	 * @return
	 */
	@RequestMapping(value="/updateName",method=RequestMethod.POST)
	@ResponseBody
	public Integer updateName(Syauthority syauthority){
		
		Integer result=	syauthorityService.updateByPrimaryKeySelective(syauthority);
		return result;
	}
	/**
	 * 插入子节点
	 * @param syauthority
	 * @return
	 */
	@RequestMapping(value="/insertChildNode",method=RequestMethod.POST)
	@ResponseBody
	public Integer insertChildNode(Syauthority syauthority){
		Integer result=0;
		Syauthority syauthority1=syauthorityService.selectByPrimaryKey(Integer.parseInt(syauthority.getPid()));
		//如果父节点的级别比等于三就不能创建子节点
		if(syauthority1.getGrade()<=2){
			syauthority.setGrade(syauthority1.getGrade()+1);//子节点的级别是父节点加1
			result=syauthorityService.insertSelective(syauthority);
			System.out.println("syauthority"+syauthority);
			if(syauthority.getId()!=null){
				result=syauthority.getId();
			}
		}else{
			result=-1;//当为-1时说明该节点等级是3，不能创建子节点
		}
		return result;
	}
	/**
	 * 移除权限
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	@ResponseBody
	public Integer remove(Integer id){
		
		Integer result=	syauthorityService.deleteByPrimaryKey(id);
		return result;
	}
	/**
	 * 添加根目录，根目录，添加id(主键自增)，pid(不能和现有id相同，否则会是该节点的子节点),
	 * 为避免添加的pid和已添加的其他节点的id相同，根目录的添加，pid和它的id相等，就避免了这个问题，
	 * 思路：通过添加name,再根据返回的主键id修改pid为id即可
	 * @param syauthority
	 * @return
	 */
	@RequestMapping(value="/insertRoot",method=RequestMethod.POST)
	@ResponseBody
	public Integer insertRoot(Syauthority syauthority){
		
		Integer result=	syauthorityService.insertSelective(syauthority);
		if(syauthority.getId()!=null){
			
			syauthority.setPid(syauthority.getId().toString());
			syauthorityService.updateByPrimaryKeySelective(syauthority);
			result=syauthority.getId();
		}
		return result;
	}
}
